﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SQLiteDatabaseUtility;
using SQLiteDatabaseUtility.ViewModel;
using System.Configuration;

namespace SQLiteDatabaseUtility.View
{
    /// <summary>
    /// Interaction logic for MainView.xaml
    /// </summary>
    public partial class MainView : Window
    {
        public MainView()
        {
            InitializeComponent();
            this.TitleTxtBlock.Text = ConfigurationManager.AppSettings["Title"];
            this.Scroller.ScrollChanged += new ScrollChangedEventHandler(Scroller_ScrollChanged);
            //this.ExecuteTxtBox.KeyDown += new KeyEventHandler(ExecuteTxtBox_KeyDown);
            this.ExecuteTxtBox.PreviewKeyDown += new KeyEventHandler(ExecuteTxtBox_KeyDown);
        }

        void ExecuteTxtBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Up)
            {
                (this.DataContext as MainViewModel).HistoryUpCommand.Execute(new object());
            }
            else if (e.Key == System.Windows.Input.Key.Down)
            {
                (this.DataContext as MainViewModel).HistoryDownCommand.Execute(new object());
            }
            
        }

        void Scroller_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            if (e.ExtentHeightChange != 0.0)
            {
                (sender as ScrollViewer).ScrollToBottom();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = new MainViewModel();
            Keyboard.Focus(this.ExecuteTxtBox);
        }
    }
}
